package com.zha;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class Biao {
    Table table = null;
    public static Connection conn = null;
    public static TableName tName = TableName.valueOf("bumen");

    @Before// 前置通知, 在方法执行之前执行
    public void init() throws IOException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "hbase:2181,hbase1:2181,hbase2:2181");
        conn = ConnectionFactory.createConnection(conf);
        table = conn.getTable(TableName.valueOf("bumen"));
    }
    @Test
    public void create() throws IOException{
        //从连接中构造一个DDL操作器
        Admin admin = conn.getAdmin();
        //if语句  如果表存在，禁用删除表
        if(admin.tableExists(tName)){
            admin.disableTable(tName);
            admin.deleteTable(tName);
        }
        //创建一个表定义描述对象
        HTableDescriptor ht = new HTableDescriptor(tName);
        //创建列族定义描述对象
        HColumnDescriptor hc = new HColumnDescriptor("cf1".getBytes());
        HColumnDescriptor hb = new HColumnDescriptor("cf2".getBytes());
        hc.setMaxVersions(5);
        hc.setBlockCacheEnabled(true);
        hc.setBlocksize(180000);

        //将列族定义信息对象放入表定义对象中
        ht.addFamily(hc);
        ht.addFamily(hb);

        //用DDL操作器对象：admin来建表
        admin.createTable(ht);
        System.out.println("表创建完成");
    }

    /**
     * 插入数据
     * @throws IOException
     */
    @Test
    public void insertData() throws IOException {
        /**
         * Bytes.toBytes()为hadoop方法,String.getBytes()为java方法。
         *toBytes()方法是将参数使用UTF-8的编码格式转换成byte[],getBytes()是用读取file.encoding的编码格式,
         * 然后用读取的格式进行转换,所以,getBytes转换的byte[]的格式取决于操作系统和用户设置,不推荐这种方法,
         * 最好统一只用toBytes()方法
         */
        Put put = new Put(Bytes.toBytes("0_001"));
        put.addColumn(Bytes.toBytes("cf1"), Bytes.toBytes("pid"), Bytes.toBytes(""));
        put.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("1_002"), Bytes.toBytes("1_002"));
        put.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("1_003"), Bytes.toBytes("1_003"));
        put.addColumn(Bytes.toBytes("cf2"), Bytes.toBytes("1_004"), Bytes.toBytes("1_004"));

        Put put2 = new Put(Bytes.toBytes("1_002"));
        put2.addColumn(Bytes.toBytes("cf1"),Bytes.toBytes("pid"),Bytes.toBytes("0_001"));
        put2.addColumn(Bytes.toBytes("cf2"),Bytes.toBytes("1_005"),Bytes.toBytes("1_005"));
        put2.addColumn(Bytes.toBytes("cf2"),Bytes.toBytes("1_006"),Bytes.toBytes("1_006"));


        table.put(put);
        table.put(put2);

        System.out.println("添加成功");
        /**
         * hbase(main):012:0> scan 'bumen'
         * ROW                                                  COLUMN+CELL
         *  0_001                                               column=cf1:pid, timestamp=1546911613922, value=
         *  0_001                                               column=cf2:1_002, timestamp=1546911613922, value=1_002
         *  0_001                                               column=cf2:1_003, timestamp=1546911613922, value=1_003
         *  0_001                                               column=cf2:1_004, timestamp=1546911613922, value=1_004
         *  1_002                                               column=cf1:pid, timestamp=1546911613988, value=0_001
         *  1_002                                               column=cf2:1_005, timestamp=1546911613988, value=1_005
         *  1_002                                               column=cf2:1_006, timestamp=1546911613988, value=1_006
         */
    }
    /**
     * 查询数据
     * 根据行键0_001查询cf1顶级部门
     */
    @Test
    public void queryData() throws IOException {
        Get get = new Get(Bytes.toBytes("0_001"));
        Result result = table.get(get);
        System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("pid"))));
    }

    /**
     * 先按行键查出0_001的数据给了get，在给result，判断getValue得到的value值是否是1_002，用  .equals()
     * @throws IOException
     */
    @Test
    public void queryDat() throws IOException {
        Get get = new Get(Bytes.toBytes("0_001"));
        Result result = table.get(get);
        if (Bytes.toString(result.getValue(Bytes.toBytes("cf2"),Bytes.toBytes("1_002"))).equals("1_002" )){
            Get get2 = new Get(Bytes.toBytes("1_002"));
            Result result2 = table.get(get2);
            System.out.println("0_001"+"  1_002  "+Bytes.toString(result2.getRow()));
        }else
            System.out.println("无法查询");
    }
}
